#include "weather.h"

lv_style_t style;


static lv_style_t bg_style;
static lv_style_t img_style;
LV_IMG_DECLARE(weather_code);
void btn_exit(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* btn = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED) {
       // lvAnimationMovUp(&a, page, LV_VER_RES);
        lv_page_load(MAIN_PAGE);
        //lvAnimationMovUp(&a, btn->parent->parent, LV_VER_RES);
    }
}
static void move(void* bar, int32_t v)
{
    lv_obj_set_x(bar, v - LV_HOR_RES);
}

void set_print(struct _lv_anim_t* ani)
{
    printf("set_print\n");
}


void lv_weather_entry_ani(lv_obj_t* obj)
{
    lv_anim_t a;

    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_values(&a, 0, LV_VER_RES);
    lv_anim_set_exec_cb(&a, setValueHorDown);
    lv_anim_set_time(&a, 1000);
    //  lv_anim_set_playback_time(&a, 2000);
    lv_anim_start(&a);
    //lvAnimationMovDown(&ani, obj, LV_VER_RES);
    lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);
}


void lv_weather_page_create(lv_obj_t* obj)
{
    lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(obj, LV_HOR_RES, LV_VER_RES);

    lv_style_init(&bg_style);
    lv_style_set_bg_opa(&bg_style, LV_OPA_COVER);
    lv_style_set_bg_color(&bg_style, lv_color_white());
    lv_obj_add_style(obj, &bg_style, 0);

    lv_style_init(&img_style);
    lv_style_set_bg_opa(&img_style, LV_OPA_30);

    lv_obj_t* img1 = lv_img_create(obj);
    lv_img_set_src(img1, &weather_code);
    lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(img1, weather_code.header.w, weather_code.header.h);


    lv_obj_t* btn = lv_btn_create(obj);
    lv_obj_set_size(btn, 100, 100);
    lv_obj_add_event_cb(btn, btn_exit, LV_EVENT_ALL, NULL);
}

void weather_msg_init(void)
{
    message_t* weather_msg = (message_t*)malloc(sizeof(message_t));
    if (weather_msg == NULL)
    {
        printf("weather_msg malloc failed\n");
        return;
    }
    // weather_msg->muxsem = xSemaphoreCreateMutex();
    // if(weather_msg->muxsem == NULL)
    //     printf("muxsem create failed");
    weather_msg->queue = QueueCreate(sizeof(struct weather_info), MAX_WEATHER_MSG);
    msg_register(WEATHER_MSG, weather_msg);
    printf("weather_msg_init\n");
}


void weather_send_data()
{
    struct weather_info beijing = {
        .city = "beijing",
        .temp = "20",
        .weather = "cloudy",
    };

    struct weather_info shagnhai = {
    .city = "shagnhai",
    .temp = "22",
    .weather = "rain",
    };
 
    msg_manager_enque(WEATHER_MSG, &beijing);
    msg_manager_enque(WEATHER_MSG, &shagnhai);

}


void weather_get_data()
{
    struct weather_info weather;
    while (!msg_manager_deque(WEATHER_MSG, &weather))
    {
        printf("city = %s,temperature = %s,weather = %s\n",weather.city,weather.temp,weather.weather);
    }
}
